<?php
App::uses( 'MemberAppModel', 'Model');

class Product extends AppModel {
    var $name = 'Product';

    var $validate = array(
        'name' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Name is not null.',
                'allowEmpty' => false,
                'required' => true,
            ),

            'maxlength' => array(
                'rule' => array('maxlength', 255),
                'message' => 'Name not more than 255 characters',
                'allowEmpty' => false,
                'required' => true,
            ),
        ),

        'image' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Image is not null.',
                'allowEmpty' => false,
                'required' => true,
            ),
        ),

        'price' => array(
            'notempty' => array(
                'rule' => array('notempty'),
                'message' => 'Price is not null',
                'allowEmpty' => false,
                'required' => true,
            ),
            'money' => array(
                'rule' => array('money'),
                'message' => 'Please, enter the price is (0-9) và (,.)',
                'allowEmpty' => false,
                'required' => false,
            ),
        ),

    );

    /**
	 * Get all
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function getAll(){
		return $this->find('all', array(
			'conditions' => array(
				'status' => array(4,5)
			)
		));
	}

	/**
	 * Get info by id
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function getInfoById($id = null){
		if(empty($id)) return array();

		return $this->find('first', array(
			'conditions' => array(
				'id' => $id,
				'status' => array(4,5)
			)
		));
	}

	/**
	 * Get all by category id
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function getAllByCate($cate_id = null){
		if(empty($cate_id)) return array();

		return $this->find('all', array(
			'fields' => array('name', 'discounts', 'desc', 'price', 'image'),
			'conditions' => array(
				'category_id' => $cate_id,
				'status' => 4
			)
		));
	}

}
